class Todo < ActiveRecord::Base
  
  belongs_to :user
  has_many :timelogs  
  has_many :notes
  
  attr_accessor :back  
  validates_presence_of :todo, :user_id, :e_type, :check_state, :repeat_days
  
  def self.find_todos_for_all(userid)
    find(:all, :conditions => ["user_id = ?", userid], :order=> "create_for_date desc, start_time")
  end 
  
  def self.find_todos_for_me(userid, options = {})    
    if options[:date]
      date = options[:date]
    else
      date = Date.today
    end
    
    find(:all, :conditions => ["user_id = ? and create_for_date = ?",  userid, date], 
    :order=> "e_type, check_state, is_full_day desc, start_time")
  end
  
  def continue    
    todo = self.clone
    self.done
    if self.create_for_date < Date.today 
      todo.create_for_date = Date.today
    else
      todo.create_for_date = self.create_for_date.to_time.tomorrow.to_date
    end 
    todo.todo += "-c"
    todo.save
    todo
  end
  
  def done
    self.check_state = true
    self.update    
  end
end
